var huiYingIdCard=function () {
  var huiYingIdCardObj = {
    ws: "",
    connected: false,
    tryonce: false,
    server_addr: "ws://127.0.0.1:18888",//本机端口地址
    file: new FileReader(),
    msg_handler:{},
    msg:"",//返回值
    context:1,
    callback:function(msg){
         //开始读身份证的回调函数，需单独处理，节省内存开销
        if(msg.err !== 0){
          if(msg.err == 150) huiYingIdCardObj.output("一个正在读卡进程正在读卡，需要先结束读卡");
          return;
        }
        //返回数据
        return msg;
      //显示读到的数据
      //huiYingIdCardObj.setText(msg);
      //显示读取数据
     // huiYingIdCardObj.output("读取身份证");
    },
    sendMsg:function(proto, msg, cb){  //向后台发送的消息处理格式
        if(!huiYingIdCardObj.ws){
          return;
        }

        if(typeof msg !== 'object'){
          if(typeof msg === 'function')
          {
            cb = msg;
            msg = {};
          }
          else{
            msg = {}
          }
        }

        msg.proto = proto;
        if(typeof cb === 'function'){
          huiYingIdCardObj.context= huiYingIdCardObj.context+1;
          huiYingIdCardObj.callback[huiYingIdCardObj.context] = cb;
          msg.user_data = huiYingIdCardObj.context;
        }
        huiYingIdCardObj.ws.send(JSON.stringify(msg));

    },output:function (content) {
      if(content.length ==0){	return;}
        var console = document.getElementById("Console");
        console.value += content;
        console.value += '\r';
        console.scrollTop = console.scrollHeight;

    },setText:function (msg) {
      let str="";
      str+="Name"+msg.strName;
      str+="Sex"+msg.strSex;
      str+="Nation"+msg.strNation;
      str+="Born"+msg.strBirth;
      str+="Address"+msg.strAddress;
      str+="CardNo"+msg.strID;
      str+="Police"+msg.strOrg;
      str+="ActivityLFrom"+msg.strVaildityfrom;
      str+="ActivityLTo"+msg.strVaildityto;
      str+="DeviceNo"+msg.strName;
      str+="PhotoBuffer"+msg.base64BMP;
      str+="PhotoDisplay"+"data:image/jpg;base64," + msg.base64BMP;
        return ;
    }
  };
  // 使用JSON
  const CLT_MSG = {   //向后台发送的命令
                      //打印文件
    PRINT_IMAGE:27,
    //读卡
    READ_IDCARD : 51,
    //开始循环读卡
    BEGIN_READ_IDCARD : 52,
    //停止循环读卡
    STOP_READ_IDCARD :53,
    //水平
    HCOPY_IDCARD :54,
    //垂直
    VCOPY_IDCARD :55,
    //正面
    PCOPY_IDCARD :56,
    //反面
    NCOPY_IDCARD :57,
  };
  return {
    FsIdCard:{
      onLoad:function (){
        //页面load消息 开启并连接后台服务
        if(huiYingIdCardObj.ws){
          return;
        }

        //output("正在连接：" + server_addr);
        huiYingIdCardObj.ws = new WebSocket(huiYingIdCardObj.server_addr);
        huiYingIdCardObj. ws.onopen = function(evt)
        {
          if(huiYingIdCardObj.connected)		{ return;}

          huiYingIdCardObj.connected = true;
          huiYingIdCardObj.output("已经连接到服务器");
        };

        huiYingIdCardObj.ws.onclose = function(evt)
        {
          if(!huiYingIdCardObj.connected)
          {
            if(!huiYingIdCardObj.tryonce)
            {
              window.location.href = "FLShell://"
              setTimeout(huiYingIdCardObj.onLoad(), 1000); //修改此处
              huiYingIdCardObj.tryonce = true;
            }
            else
            {
              setTimeout(huiYingIdCardObj.onLoad(), 1000);
            }
          }
          else
          {
            huiYingIdCardObj.output("已断开服务器连接");
            huiYingIdCardObj.connected = false;
          }

          huiYingIdCardObj.ws = null;
        }

        huiYingIdCardObj.ws.onmessage = function(evt)
        {
          if(evt.data.length === 0 ){
            return;
          }

          if(typeof evt.data === 'string'){
            var msg = JSON.parse(evt.data);
            if(msg.err && msg.err !== 0){
              huiYingIdCardObj.output("错误 - " + msg.err + ", " + msg.msg);
            }
            else if(msg.msg){
              huiYingIdCardObj.output(msg.msg);
            }

            if(msg.user_data){
              huiYingIdCardObj.callback[msg.user_data](msg);
              if(typeof msg.user_data === 'number'){
                huiYingIdCardObj.callback[msg.user_data] = undefined;
              }
            }
          }
          else{
            var file = new FileReader();
            file.onload = function(evt) {
              if(evt.target.readyState == FileReader.DONE){
                var data = new Uint8Array(evt.target.result);
                if(data[0] == SVR_BIN_MSG.CAP_FRAME){
                  return drawImage(data);
                }
              }
            };

            file.readAsArrayBuffer(evt.data);
          }
        }
      },sendMsg:function(proto, msg, cb){  //向后台发送的消息处理格式
    if(!huiYingIdCardObj.ws){
      return;
    }

    if(typeof msg !== 'object'){
      if(typeof msg === 'function')
      {
        cb = msg;
        msg = {};
      }
      else{
        msg = {}
      }
    }
    msg.proto = proto;
    if(typeof cb === 'function'){
      huiYingIdCardObj.callback[++(huiYingIdCardObj.context)] = cb;
      msg.user_data = huiYingIdCardObj.context;
    }
    huiYingIdCardObj.ws.send(JSON.stringify(msg));
  },ReadIDCard: function ReadIDCard() { //读身份证
    //清除缓存数据
    huiYingIdCardObj.msg="";
    return huiYingIdCardObj.sendMsg(CLT_MSG.READ_IDCARD, function(msg){
      if(msg.err !== 0){ return;}
      //返加读到的消息
      //返回格式
      let resultMsg={
        sex:msg.strSex,
        name:msg.strName,
        nation:msg.strNation,
        birthDate:msg.strBirth,
        address:msg.strAddress,
        idCardNo:msg.strID,
        photoDisplay:msg.base64BMP
        };
      huiYingIdCardObj.msg=resultMsg;
      return huiYingIdCardObj.msg;
      // huiYingIdCardObj.setText(msg);
      // alert();
      // huiYingIdCardObj.output("单读卡");
    });

  },//返回读到的数据
      getMsg:function () {
        return  huiYingIdCardObj.msg;
      },Begin: function (){ //开始读身份证
        huiYingIdCardObj.sendMsg(CLT_MSG.BEGIN_READ_IDCARD, {user_data:"idcard"});
        huiYingIdCardObj.output("开始读卡");
  },Stop: function  (){ //停止读身份证
        huiYingIdCardObj.sendMsg(CLT_MSG.STOP_READ_IDCARD, function(msg){
      if(msg.err !== 0){return;}
      huiYingIdCardObj.output("停止读卡");
    });
  },HCopy:function (){ //横向复印
        huiYingIdCardObj. sendMsg(CLT_MSG.HCOPY_IDCARD, {copy_path:"D:\\"},function(msg){
      if(msg.err !== 0){return;}
      document.getElementById("IDCardDisplay").src = "data:image/jpg;base64," + msg.hcopy;

      sendMsg(CLT_MSG.PRINT_IMAGE,{file:msg.filename},function(msg2){
        if(msg2.err !== 0){ return;}
        huiYingIdCardObj.output("打印作业号: " + msg2.print_no);
      });
          huiYingIdCardObj.output("横向复印" + msg.filename);
    });
  },VCopy: function  (){ //竖向复印
    sendMsg(CLT_MSG.VCOPY_IDCARD, {copy_path:"D:\\"},function(msg){
      if(msg.err !== 0){return;}
      document.getElementById("IDCardDisplay").src = "data:image/jpg;base64," + msg.vcopy;
      huiYingIdCardObj.sendMsg(CLT_MSG.PRINT_IMAGE,{file:msg.filename},function(msg2){
        if(msg2.err !== 0){ return;}
        huiYingIdCardObj.output("打印作业号: " + msg2.print_no);
      });
      huiYingIdCardObj.output("竖向复印" + msg.filename);
    });
  },PCopy:function(){ //正面复印
    sendMsg(CLT_MSG.PCOPY_IDCARD, {copy_path:"D:\\"},function(msg){
      if(msg.err !== 0){return;}
      document.getElementById("IDCardDisplay").src = "data:image/jpg;base64," + msg.pcopy;
      sendMsg(CLT_MSG.PRINT_IMAGE,{file:msg.filename},function(msg2){
        if(msg2.err !== 0){ return;}
        huiYingIdCardObj.output("打印作业号: " + msg2.print_no);
      });
      huiYingIdCardObj.output("正面复印" + msg.filename);
    });
  },NCopy: function (){ //反面复印
    sendMsg(CLT_MSG.NCOPY_IDCARD, {copy_path:"D:\\"},function(msg){
      if(msg.err !== 0){return;}
      document.getElementById("IDCardDisplay").src = "data:image/jpg;base64," + msg.ncopy;
      sendMsg(CLT_MSG.PRINT_IMAGE,{file:msg.filename},function(msg2){
        if(msg2.err !== 0){ return;}
        huiYingIdCardObj.output("打印作业号: " + msg2.print_no);
      });
      huiYingIdCardObj.output("反面复印" + msg.filename);
    });
     }
    }
  };

}();
export default huiYingIdCard;
